package server;

import java.util.Vector;

/**
 * A class that holds the shared data structures and methods for working with resources and jobs:
 * holds the nonSubmittedJobs, submittedJobs & finishedJobs Vectors as well as the resources Vector
 * @author yariv zur
 *
 */
public class Globals {

	private static int jobCounter;
	static Vector<Job> nonSubmittedJobs, submittedJobs, finishedJobs;
	static Vector<Resource> resources;
	static String PATH ; // the "photos" folder path in the file system
	final static String xhtmlHeader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\r\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n";
	final static String xmlnsHeader = "<html xmlns= \"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang= \"en\">\r\n";
	final static String m200 = "HTTP/1.1 200 OK\r\n";
	final static String m201 = "HTTP/1.1 201 Created\r\n";
	final static String m202 = "HTTP/1.1 202 Accepted\r\n";
	final static String m204 = "HTTP/1.1 204 No Content\r\n";
	final static String m206 = "HTTP/1.1 206 Partial Content\r\n";
	final static String m400 = "HTTP/1.1 400 Bad Request\r\n";
	final static String m404 = "HTTP/1.1 404 Not Found\r\n";
	final static String m500 = "HTTP/1.1 500 Internal Server Error\r\n";
	static boolean SHUTDOWN = false; //the shutdown indicator
	final static MimeType mt = new MimeType("mime.types");

	/**
	 * Creates a new job and adds it to the nonSubmittedJobs Vector
	 * @return the new job created
	 */
	static Job addNewJob(){
		synchronized(nonSubmittedJobs){
			Job j = new Job(""+(jobCounter++));
			nonSubmittedJobs.addElement(j);
			return j;
		}
	}
	/**
	 * returns the first available job in the nonSubmittedJobs, or null if there are no jobs available
	 * @return the next job available
	 */
	static Job getNewJob(){
		Job ans = null;
		synchronized(nonSubmittedJobs){
			if(nonSubmittedJobs.size()>0){
				ans = nonSubmittedJobs.remove(0);
				synchronized(submittedJobs){
					submittedJobs.add(ans);
				}
				ans.setStatus("submitted");
			}
		}
		return ans;
	}
	/**
	 * moves a submitted job to the finishedJobs Vector after it's output representation has been uploaded to the server
	 * @param jobId - the job to move
	 */
	static void moveToFinished(String jobId){
		synchronized(submittedJobs){
			for(Job j: submittedJobs){
				if (j.getJobId().equals(jobId)){
					submittedJobs.remove(j);
					synchronized(finishedJobs){
						finishedJobs.add(j);
					}
					j.setStatus("finished");
					break;
				}
			}
		}
	}
	/**
	 * moves a new job to the submittedJobs Vector after it has been given to an employee
	 * @param jobId - the job to move
	 */
	static void moveToSubmitted(String jobId){
		synchronized(nonSubmittedJobs){
			for(Job j: nonSubmittedJobs){
				if (j.getJobId().equals(jobId)){
					int index = nonSubmittedJobs.indexOf(j);
					synchronized(submittedJobs){
						submittedJobs.add(nonSubmittedJobs.remove(index));
					}
					j.setStatus("submitted");
					break;
				}
			}
		}
	}


	/**
	 * Adds a new resource to the resources Vector and returns it
	 * @return the new Resource created
	 */
	static synchronized Resource addResource(){
		Resource res = new Resource(""+resources.size());
		resources.addElement(res);
		return res;
	}
	/**
	 * Finds a specific Resource in the resource Vector by name and returns it
	 * @param id - the resource to look for
	 * @return the resource found, or null
	 */
	static synchronized Resource findResource(String id){
		Resource ans = null;
		for(Resource r: Globals.resources){
			if(r.getId().equals(id)){
				ans = r;
				break;
			}
		}
		return ans;
	}

}



